from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

from dama.dama import Dama

# 连接浏览器
driver=webdriver.Edge()
# 窗口最大化
driver.maximize_window()
# 找到浏览器
driver.get('http://10.59.9.12:7001/')
time.sleep(3)
# 定位登录
driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
time.sleep(3)
# 定位用户名
driver.find_element(By.NAME,'username').send_keys('13811033962')
time.sleep(3)
# 定位密码
driver.find_element(By.NAME,'password').send_keys('123456')
time.sleep(6)
# 打码 验证码 将验证码图片上的内容提取出来
datu=driver.save_screenshot('datu.png')
yzmtu=driver.find_element(By.XPATH,'//*[@id="theForm"]/div[3]/span[3]/img')
# 获取验证码位置
location=yzmtu.location
print(location)
# 大小
size = yzmtu.size
print(size)
left=location['x']
top=location['y']
right=left+size['width']
bottom=top+size['height']
# 截取 验证码位置的图片
image = Image.open('datu.png')

imagex = image.crop((left*1.5,top*1.5,right*1.5,bottom*1.5))
imagex.save('imageyzm.png')


dm = Dama('ls13012558376','9h0nh6ee','')
yzmdm = open('imageyzm.png','rb').read()
jieguo = dm.PostPic(yzmdm,1902)
print(jieguo)
result = jieguo['pic_str']
time.sleep(2)

driver.find_element(By.NAME,'code').send_keys(result)
# 点击登录按钮
driver.find_element(By.XPATH,'//*[@id="theForm"]/div[4]/button').click()
time.sleep(10)









# # 进行登录
# driver.find_element(By.CLASS_NAME,'finish-btn').click()
# time.sleep(10)
# # 断言
# dy1=driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').text
# print(dy1)
#
# # 搜索
# driver.find_element(By.CLASS_NAME,'search-input').send_keys('茅台')
# time.sleep(3)
# driver.find_element(By.CLASS_NAME,'search-input-btn').click()
# time.sleep(3)
# driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/ul/li[1]/div[1]/span/a/img').click()
# time.sleep(3)
# driver.find_element(By.XPATH,'//*[@id="buy-cart"]').click()
# time.sleep(3)
# driver.find_element(By.XPATH,'//*[@id="address-801"]').click()
# time.sleep(3)
# driver.find_element(By.CLASS_NAME,'ask-btn').click()
# time.sleep(3)